import sys

# 定义四个方向的增量，分别表示：水平、垂直、对角线、反对角线
DIRECTIONS = [
    (0, 1), (1, 0), (1, 1), (-1, 1)
]

def getMaxConnected(students, row, column, res):
    m = len(students)
    n = len(students[0])

    for dir in DIRECTIONS:
        len_m = 1  # 初始化连续的M的个数为1
        a, b = row, column

        # 按当前方向搜索
        while (0 <= a + dir[0] < m) and (0 <= b + dir[1] < n) and students[a + dir[0]][b + dir[1]] == "M":
            a += dir[0]  # 更新行索引
            b += dir[1]  # 更新列索引
            len_m += 1  # 连续的M的个数加1

        res.append(len_m)  # 把连续的M的个数加入结果数组

if __name__ == "__main__":
    input_str = input().strip()
    row, column = map(int, input_str.split(","))

    # 初始化方阵
    students = []
    for _ in range(row):
        student_str = input().strip()
        students.append(student_str.split(","))

    max_res = []  # 初始化结果数组
    for i in range(row):
        for j in range(column):
            if students[i][j] == "M":
                getMaxConnected(students, i, j, max_res)  # 在四个方向上搜索连续的M

    max_res.sort(reverse=True)  # 对结果数组排序
    print(max_res[0])  # 输出最大的连续的M的个数